﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.Mvc;
using System.Web.Routing;
using Rhino.Commons;

namespace WebUI
{
    public class WindsorControllerFactory : IControllerFactory
    {

        #region IControllerFactory Members

        public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            var ret = (IController)IoC.Resolve(controllerName + "Controller");

            Debug.Assert(ret != null, string.Format("The controller with name '{0}' could not be resolved from the container.", controllerName));

            return ret;
        }

        public void ReleaseController(IController controller)
        {
            var disposable = controller as IDisposable;

            if (disposable != null)
            {
                disposable.Dispose();
            }

            IoC.Container.Release(controller);
        }

        #endregion
    }
}